//图片验证码切换
function changeImg() {
    document.getElementById("img").src = "/imageCode?r=" + Math.random();
}

layer = layui.layer

var register = new Vue({
    el: ".layui-form",
    data: {
        username: '',
        password: '',
        imagecode: '',
    },
    methods: {
        submit: function() {
            that = this
            var code = $.cookie('login_code')
                // console.log(code, this.imagecode)
            if (!code) {
                layer.msg('验证码失效，请刷新验证码', {
                    icon: 2,
                    time: 1.5 * 1000,
                    shift: 6
                });
                return
            }
            if (code != this.imagecode) {
                layer.msg('验证码错误', {
                    icon: 2,
                    time: 1.5 * 1000,
                    shift: 6
                });
                return
            }
            let param = new URLSearchParams()
            param.append('username', this.username)
            param.append('password', this.password)
            axios.post("http://127.0.0.1:8000/login/", param).then(
                function(response) {
                    var resp = response.data
                    console.log(resp)
                    var msg = resp['content']
                    if (resp.response) {
                        image = $.cookie('image')
                        name = $.cookie('name')
                        login = $.cookie('login')
                        authority = $.cookie('authority')
                        console.log(image, name, login, authority)
                        layer.alert(msg && msg || '登录成功', {
                            icon: 1,
                            skin: 'layer-ext-demo', //见：扩展说明
                            yes: function() {
                                login = $.cookie('login')
                                authority = $.cookie('authority')
                                if (login) {
                                    return window.location.href = '/static/backindex.html'

                                } else {
                                    layer.alert('服务端错误', {
                                        icon: 2,
                                        skin: 'layer-ext-demo' //见：扩展说明
                                    })
                                }
                            }
                        })

                    } else {
                        layer.alert(msg && msg || '登录失败', {
                            icon: 2,
                            skin: 'layer-ext-demo' //见：扩展说明
                        })
                    }

                },
                function(err) {
                    console.log(err)
                    layer.alert('登录失败', {
                        icon: 2,
                        skin: 'layer-ext-demo' //见：扩展说明
                    })
                }
            )



        }
    }
})